#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> prices(n);

    for (int i = 0; i < n; i++) {
        cin >> prices[i];
    }

    m = min(n / 2, m);
    vector<vector<int>> f(n, vector<int>(m + 1, -0x3f3f3f3f));
    auto g = f;
    f[0][0] = -prices[0], g[0][0] = 0;

    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= m; j++) {
            f[i][j] = max(f[i - 1][j], g[i - 1][j] - prices[i]);
            g[i][j] = g[i - 1][j];
            if (j >= 1)
                g[i][j] = max(g[i][j], f[i - 1][j - 1] + prices[i]);
        }
    }

    int ans = 0;
    for (int i = 0; i <= m; i++) {
        ans = max(ans, g[n - 1][i]);
    }
    cout << ans << endl;

    return 0;
}